package com.twu.biblioteca;

import com.twu.biblioteca.entity.Book;
import com.twu.biblioteca.entity.Movie;
import com.twu.biblioteca.entity.User;
import com.twu.biblioteca.impl.*;

import java.util.ArrayList;
import java.util.List;

public class BibliotecaApp {

    public static void main(String[] args) {
        MainMenu mainMenu = new MainMenu();
        OperateBook operateBook = new OperateBook();
        ViewMovie viewMovie = new ViewMovie();
        Login login = new Login();
        UserInfo userInfo = new UserInfo();
        String usernameInputted = "";
        String passwordInputted = "";

        for (boolean goOnMainMenu = true; goOnMainMenu != false; )
        {
            mainMenu.showTheMainMenu();
            String theValueOfCustomerChoice = mainMenu.getTheChoice();
            String theMayResultOfCustomerChoice = mainMenu.checkTheChoiceOfMainMenu(theValueOfCustomerChoice);

            if (theMayResultOfCustomerChoice.equals("showTheListOfAllBooks"))
            {
                for (boolean goOnSelectBook = true; goOnSelectBook != false; )
                {
                    List<Book> bookList = new ArrayList<Book>(7);
                    for(int i = 0; i < 7; i++)
                    {
                        Book bookTemp = new Book();
                        StringBuffer bookTitleTemp = new StringBuffer();
                        bookTitleTemp.append("book_").append(i);

                        bookTemp.setBookId(String.valueOf(i));
                        bookTemp.setBookTitle(bookTitleTemp.toString());
                        if (i == 1 || i == 3 || i == 6)
                            bookTemp.setBookState("Available");
                        else
                            bookTemp.setBookState("Unavailable");
                        bookList.add(i, bookTemp);
                    }
                    operateBook.showTheListOfAllBooks(bookList);
                    if ((!usernameInputted.equals("")) && (!passwordInputted.equals("")))
                    {
                        String theValueOfSelectedBookId = operateBook.getSelectedChoiceId();
                        String theMayResultOfSelectedBookId = operateBook.selectBook(theValueOfSelectedBookId);

                        if (theMayResultOfSelectedBookId.equals("redo"))
                            continue;
                        else if(theMayResultOfSelectedBookId.equals("goToMainMenu"))
                            break;
                        else if(theMayResultOfSelectedBookId.equals("exit"))
                            System.exit(1);
                        else
                            continue;
                }
                    else
                    {
                        System.out.println(">> Please login!");
                        break;
                    }
                }
            }
            else if(theMayResultOfCustomerChoice.equals("showTheListOfMovies")) {
                List<Movie> movieList = new ArrayList<Movie>(15);
                for(int i = 0; i < 15; i++)
                {
                    Movie movieTemp = new Movie();
                    StringBuffer movieNameTemp = new StringBuffer();
                    movieNameTemp.append("movie").append(i);

                    movieTemp.setMovieName(movieNameTemp.toString());
                    movieTemp.setYear("2001");
                    movieTemp.setDirector("Jack");
                    if (i >= 0 && i <= 10)
                        movieTemp.setRating(i);
                    else if (i > 10)
                        movieTemp.setRating(i-9);
                    movieList.add(i, movieTemp);
                }
                for (boolean goOnViewMovie = true; goOnViewMovie != false; ) {
                    viewMovie.showTheListOfMovie(movieList);
                    String theValueOfNextOperation = viewMovie.getNextOperation();
                    if (theValueOfNextOperation.equals("return"))
                        break;
                    else if (theValueOfNextOperation.equals("exit"))
                        System.exit(1);
                    else
                        System.out.println(">> Select a valid option!!   ");
                }
            }
            else if(theMayResultOfCustomerChoice.equals("showTheMainMenu"))
                continue;
            else if(theMayResultOfCustomerChoice.equals("login")) {
                if ((!usernameInputted.equals("")) && (!passwordInputted.equals("")))
                {
                    System.out.println(">> You are logged in! \n>> If you want to login again, please exit!\n");
                }
                else {
                    List<User> userList = new ArrayList<User>(10);
                    for(int i = 111; i < 119; i++)
                    {
                        User userTemp = new User();
                        StringBuffer usernameTemp = new StringBuffer();
                        usernameTemp.append(i).append("-1111");
                        userTemp.setUsername(usernameTemp.toString());
                        userTemp.setPassword("123");
                        userTemp.setName("John");
                        StringBuffer emailTemp = new StringBuffer();
                        emailTemp.append(i).append("@gmail.com");
                        userTemp.setEmail(emailTemp.toString());
                        userTemp.setPhone(String.valueOf(i));
                        userList.add(i-111, userTemp);
                }

                List<String> userInput = login.getUserInput();
                String usernameInput = userInput.get(0);
                String passwordInput = userInput.get(1);
                usernameInputted = usernameInput;
                passwordInputted = passwordInput;
                login.confirmUserInfo(usernameInput, passwordInput, userList);
            }
            }
            else if(theMayResultOfCustomerChoice.equals("showTheUserInfo")) {
                if ((!usernameInputted.equals("")) && (!passwordInputted.equals("")))
                {
                    userInfo.showUserInfo(usernameInputted);
                }
                else
                {
                    System.out.println(">> Please talk to a Librarian. Thank you.\n");
                }
            }
            else if(theMayResultOfCustomerChoice.equals("exit"))
                break;
            else
                continue;
        }
    }
}
